package com.cityfood.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.AdminProductDisplay;
import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.CommentStat;
import com.cityfood.common.pojo.ProductDisplay;
import com.cityfood.common.pojo.ProductSpeceDisplay;
import com.cityfood.common.pojo.SimpleProduct;
import com.cityfood.common.utils.JsonUtils;
import com.cityfood.common.utils.ProductNoUtil;
import com.cityfood.pojo.CfComment;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfProductDetail;
import com.cityfood.pojo.CfProductSpecs;
import com.cityfood.pojo.CfUser;
import com.cityfood.pojo.CfUserCollect;
import com.cityfood.service.CommentService;
import com.cityfood.service.ProductDetailService;
import com.cityfood.service.ProductService;
import com.cityfood.service.ProductSpecsService;
import com.cityfood.service.UserCollectService;
import com.cityfood.service.UserService;
import com.github.pagehelper.PageInfo;

@Controller
public class ProductController {
	//商品接口
	@Autowired
	private ProductService productService;
	//商品规格接口
	@Autowired
	private ProductSpecsService specsService;
	//商品详情接口
	@Autowired
	private ProductDetailService detailService;
	//商品评论接口
	@Autowired
	private CommentService commentService;
	//用户接口
	@Autowired
	private UserService userService;
	//用户收藏接口
	@Autowired
	private UserCollectService collectService;
	
	//根据商品id查找商品信息
	@RequestMapping(value="/product/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getProductById(@PathVariable(value="id") Integer productId,
											@RequestParam Integer userId){
		
		CityFoodResult cityFoodResult = new CityFoodResult(); 
		//商品展示包装类
		Map<String,Object> productDisplay = new LinkedHashMap<>();
		//根据商品id查询商品
		CfProduct product = productService.getProductByProductId(productId);
		if (product==null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("此商品已下架");
			cityFoodResult.setData("{}");
		}else{
			if (product.getIsdisplay()==0) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("此商品已下架");
				cityFoodResult.setData("{}");
				return cityFoodResult;
			}
			List<CfProductSpecs> productSpecs = specsService.getProductSpecsByProductId(productId);
			if (productSpecs == null) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("此商品无规格");
				cityFoodResult.setData("{}");
			}
			productDisplay.put("foodId", product.getId());
			productDisplay.put("foodName", product.getName());
			//计算好评率
			List<CfComment> commentList = commentService.getCommentByProductId(product.getId());
			//默认100 好评
			Integer probability=100;
			if (commentList.size()>0) {
				//差评人数
				Integer negative_comment =0;
				for (CfComment comment : commentList) {
					if (comment.getCommentNumber()==1) {
						negative_comment++;
					}
				}
				probability=(commentList.size()-negative_comment)/commentList.size()*100;
			}
			//商品好评率
			productDisplay.put("probability", probability);
			//促销活动
			List<String> promotions = new ArrayList<>();
			productDisplay.put("promotions", promotions);
			productDisplay.put("pic", product.getSubImage());
			//Integer转换BigDecimal
			BigDecimal price  = new BigDecimal("0.00");
			CfProductSpecs specs = new CfProductSpecs();
			if (productSpecs!=null) {
				specs=productSpecs.get(0);
			}
			if (specs.getPrice()!=null) {
				price = new BigDecimal(specs.getPrice().toString());
				price.setScale(2, RoundingMode.FLOOR);
			}
			productDisplay.put("price", price);
			CfUserCollect collect = collectService.getCollectByUserIdAndProductId(userId, productId);
			if (collect==null) {
				productDisplay.put("isCollect", 0);
			}else{
				productDisplay.put("isCollect", 1);
			}
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询成功");
			cityFoodResult.setData(productDisplay);
		}
		return cityFoodResult;
	}
	
	
	
	//根据商品id查找商品信息
		@RequestMapping(value="/products/toPayProductList",method=RequestMethod.GET)
		@ResponseBody
		public CityFoodResult getProductById(HttpServletRequest req,String specsIds){
			CityFoodResult cityFoodResult = new CityFoodResult(); 
			
			String[] ids = specsIds.split(",");
			List<Map<String, Object>> ls = new  ArrayList<Map<String,Object>>();
			for (String id : ids) {
				CfProductSpecs specs = specsService.getProductSpecsBySpecsId(Integer.parseInt(id));
				Map<String,Object> mp = new LinkedHashMap<>();
				mp.put("specId", specs.getId());
				CfProduct product = productService.getProductByProductId(specs.getProductId());
				mp.put("specName", product.getName());
				mp.put("specPic", product.getMainImage());
				HttpSession session = req.getSession();
				List<Map<String,Integer>> buyCart = (List<Map<String,Integer>>)session.getAttribute("buyCart");
				double num = 0;
				if (buyCart!=null) {
					for (Map<String, Integer> map : buyCart) {
						if (map.get("specsId")==Integer.parseInt(id)) {
							num=map.get("num");
							mp.put("num", num);
						}
					}
				}else{
					mp.put("num", num);
				}
				//计算总价
				BigDecimal price = specs.getPrice();
				mp.put("price", price.doubleValue()*num);
				Map<String,String> specAttrs = JsonUtils.jsonToMap(specs.getProductSpecs());
				String taste = "";
				for (String str : specAttrs.values()) {
					taste=str+"/"+taste;
				}
				taste=taste.substring(0, taste.length()-1);
				mp.put("specAttrs", taste);
				ls.add(mp);
			}
			if (ls.size()>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("查询商品规格成功");
				cityFoodResult.setData(ls);
				return cityFoodResult;
			}
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("暂无商品规格");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
	
	//后台管理---------------------------------------------------------------------------
	
	//根据商品条件查找商品信息
		@SuppressWarnings("unchecked")
		@RequestMapping(value="/manage/product",method=RequestMethod.GET)
		@ResponseBody
		public CityFoodResult getProductByLikeName(@RequestParam(defaultValue="") String productMap,
												   @RequestParam(defaultValue="1") Integer currentPage) throws UnsupportedEncodingException{
			CityFoodResult cityFoodResult = new CityFoodResult(); 
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			
			String conditonsMap = URLDecoder.decode(productMap,"UTF-8");
			
			Map<String,Object> pMap = new LinkedHashMap<>();
			if (!conditonsMap.equals("{}")) {
				pMap = (Map<String,Object>)JsonUtils.jsonToMap(conditonsMap);
			}
			
			//根据商品id查询商品
			PageInfo<CfProduct> productList = productService.getProductByCategoryIdAndPage(pMap,currentPage,20);
			
			//List<CfProductSpecs> productSpecs = specsService.getProductSpecsByProductId(productId);
			if (productList==null) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("此商品已下架");
				cityFoodResult.setData("{}");
			}else{
				//商品展示包装类
				List<AdminProductDisplay> apDisplayList = new ArrayList<AdminProductDisplay>();
				for (CfProduct product : productList.getList()) {
					CfProductDetail detail = detailService.getProductDetailByProductId(product.getId());
						AdminProductDisplay apDisplay = new AdminProductDisplay();
						apDisplay.setProductId(product.getId());
						apDisplay.setCategoryId(product.getCategoryId());
						apDisplay.setProductNo(product.getProductNo());
						apDisplay.setName(product.getName());
						apDisplay.setTitle(product.getSubTitle());
						apDisplay.setMainImage(product.getMainImage());
						apDisplay.setSubImage(product.getSubImage());
						apDisplay.setIsDisplay((int)product.getIsdisplay());
						Map attributeList = null;
						if (product.getAttributeList()!=null) {
							attributeList = JsonUtils.jsonToMap(product.getAttributeList());
						}
						apDisplay.setAttribute(attributeList);
						apDisplay.setDetail(detail.getDetail());
						apDisplay.setCreateTime(sd.format(product.getCreateTime()));
						apDisplayList.add(apDisplay);
				}
				Map<String,Object> mp = new LinkedHashMap<>();
				//存入查询到的总数
				mp.put("total", productList.getTotal());
				//存入需要展示的数据
				mp.put("products", apDisplayList);
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("查询成功");
				cityFoodResult.setData(mp);
			}
			return cityFoodResult;
		}
	
	//根据商品信息添加商品，insert
	@RequestMapping(value="/manage/product",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertProduct(@RequestBody Map<String,Object> map){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		String name = (String)map.get("name");
		String subTitle = (String)map.get("subTitle");
		String mainImage = (String)map.get("mainImage");
		String subImage = (String)map.get("subImage");
		String attributeList = (String)map.get("attributeList");
		String detail = (String)map.get("detail");
		Integer categoryId = Integer.parseInt(map.get("categoryId").toString());
		
		
		CfProduct product = new CfProduct();
		product.setName(name);
		product.setProductNo(ProductNoUtil.getProductNo());
		product.setSubTitle(subTitle);
		product.setMainImage(mainImage);
		product.setSubImage(subImage);
		product.setAttributeList(attributeList);
		product.setCategoryId(categoryId);
		//添加商品编号
		product .setProductNo(ProductNoUtil.getProductNo());
		int i = productService.insertProduct(product);
		
		CfProductDetail productDetail = new CfProductDetail();
		productDetail.setProductId(product.getId());
		productDetail.setDetail(detail);
		productDetail.setIsdisplay((byte)1);
		int j = detailService.insertProductDetail(productDetail);
		if (i>0&&j>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("添加商品成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("添加商品失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	//根据商品信息修改商品,update
	@RequestMapping(value="/manage/product",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateProduct(@RequestBody Map<String,Object> map){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		Integer id = Integer.parseInt(map.get("productId").toString());
		String name = (String)map.get("name");
		String subTitle = (String)map.get("subTitle");
		String mainImage = (String)map.get("mainImage");
		String subImage = (String)map.get("subImage");
		String attributeList = (String)map.get("attributeList");
		String detail = (String)map.get("detail");
		Integer categoryId = Integer.parseInt(map.get("categoryId").toString());
		
		
		CfProduct product = new CfProduct();
		product.setId(id);
		product.setName(name);
		product.setSubTitle(subTitle);
		product.setMainImage(mainImage);
		product.setSubImage(subImage);
		product.setAttributeList(attributeList);
		product.setCategoryId(categoryId);
		
		int i = productService.updateProduct(product);
		CfProductDetail productDetail = detailService.getProductDetailByProductId(id);
		if (productDetail!=null) {
			productDetail.setDetail(detail);
			//更新商品详情
			detailService.updateProductDetail(productDetail);
		}
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("修改商品成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("修改商品失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	//根据商品信息删除商品,delete
	@RequestMapping(value="/manage/product/remove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteProduct(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		CfProduct product=new CfProduct();
		product.setId(Integer.parseInt(id));
		int i = productService.deleteProduct(product);
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("删除商品成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("删除商品失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	// 根据商品信息删除商品,delete
	@RequestMapping(value = "/manage/product/batchremove", method = RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteProducts(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			
			CfProduct product = new CfProduct();
			product.setId(Integer.parseInt(strId));
			
			int i = productService.deleteProduct(product);
			if (i > 0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除商品成功");
				cityFoodResult.setData(true);
			} else {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除商品失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}
		return cityFoodResult;
	}
	
	
	//前端查询--------------------------------------------------------------
	
	//根据商品id查找商品规格信息
	@RequestMapping(value="/product/specs",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getProductSpecsById(Integer productId){	
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		//商品口味展示包装类集合
		List<ProductSpeceDisplay> speceDisplays = new ArrayList<ProductSpeceDisplay>();
		//根据商品id查询商品的所有规格
		List<CfProductSpecs> specses = specsService.getProductSpecsByProductId(productId);
		if (specses==null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("查询商品规格失败");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		
		for (CfProductSpecs specs : specses) {
			//判断是否下架
			if (specs.getIsdisplay()==1) {
				//商品口味展示包装类
				ProductSpeceDisplay speceDisplay = new ProductSpeceDisplay();
				speceDisplay.setSpeceId(specs.getId());
				CfProduct product = productService.getProductByProductId(specs.getProductId());
				speceDisplay.setProductPic(product.getMainImage());
				speceDisplay.setPrice(specs.getPrice().doubleValue());
				Map<String,String> tasteMap = (Map<String,String>)JsonUtils.jsonToMap(specs.getProductSpecs());
				
				String taste = "";
				for (String str : tasteMap.values()) {
					taste=str+"/"+taste;
				}
				taste=taste.substring(0, taste.length()-1);
				speceDisplay.setAttrs(taste);
				speceDisplay.setNumber(specs.getStock());
				speceDisplay.setIsdisplay(specs.getIsdisplay());
				//商品口味展示包装类speceDisplay放到集合里面
				speceDisplays.add(speceDisplay);
			}
		}
		if (speceDisplays.size()>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询商品规格成功");
			cityFoodResult.setData(speceDisplays);
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("查询商品口味失败");
		cityFoodResult.setData("{}");
		return cityFoodResult;
	}
	
	
	
	//前台查询------------------------------------------------------------------------
	
	//根据商品id获取商品的属性集合
	@RequestMapping(value="/product/attribute/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getProductAttribute(@PathVariable(value="id") Integer productId){
		CityFoodResult cityFoodResult = new CityFoodResult();
		//根据商品id获取商品
		CfProduct product = productService.getProductByProductId(productId);
		if (product.getIsdisplay()==0) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("此商品已下架");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		//根据商品的属性集合，使用JsonUtils转为map集合
		Map<String, Object> foodAttribute = null;
		//商品属性为空就直接返回查询商品参数失败
		if (product!=null) {
			if (product.getAttributeList()!=null) {
				foodAttribute = JsonUtils.jsonToMap(product.getAttributeList());
				//判断foodAttribute是否有参数
				if (!foodAttribute.isEmpty()) {
					
					Map<String,Object> displayAttr = new LinkedHashMap<>();
					
					for (String key : foodAttribute.keySet()) {
						List valueList = (List)foodAttribute.get(key);
						String value = "";
						if(valueList.size() < 2){
							displayAttr.put(key, value);
						}else{
							for (Object object : valueList) {
								value=object+"/"+value;
							}
							displayAttr.put(key, value.substring(0,value.length()-1));
						}
					
					}
					
					cityFoodResult.setCode(200);
					cityFoodResult.setMsg("查询商品参数成功");
					cityFoodResult.setData(displayAttr);
					return cityFoodResult;
				}
			}else{
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("暂无商品参数");
				cityFoodResult.setData("{}");
				return cityFoodResult;
			}
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("查询商品参数失败");
		cityFoodResult.setData("{}");
		return cityFoodResult;
	}
	
	
	//前台查询---------------------------------------------------------------------
	
	//根据商品id查找商品详情信息
	@RequestMapping(value="/product/detail/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getProductDetailByProductId(@PathVariable(value="id") Integer productId){
		CityFoodResult cityFoodResult = new CityFoodResult();
		//根据商品id查询商品详情
		CfProductDetail detail = detailService.getProductDetailByProductId(productId);
		if (detail.getDetail()!=null) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询商品详情成功");
			String[] split = detail.getDetail().split(",");
			List<String> list = new ArrayList<>();
			for (String string : split) {
				list.add(string);
			}
			cityFoodResult.setData(list);
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("暂无商品详情~");
		cityFoodResult.setData("{}");
		return cityFoodResult;
	}
	
	
	//后台管理---------------------------------------------------------------------------
	
	// 根据商品详情信息添加商品详情，insert
	@RequestMapping(value = "/manage/product/detail", method = RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertProductDetail(@RequestBody Map<String,Object> map) {
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		Integer productId = Integer.parseInt(map.get("productId").toString());
		String detail = (String)map.get("detail");
		
		CfProductDetail productDetail = new CfProductDetail();
		productDetail.setProductId(productId);
		productDetail.setDetail(detail);
		productDetail.setIsdisplay((byte)1);
		productDetail.setCreateTime(new Date());
		productDetail.setUpdateTime(new Date());
		
		int i = detailService.insertProductDetail(productDetail);
		if (i > 0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("添加商品详情成功");
			cityFoodResult.setData(true);
		} else {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("添加商品详情失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}

	// 根据商品详情信息修改商品详情,update
	@RequestMapping(value = "/manage/product/detail", method = RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateProductDetail(@RequestBody Map<String,Object> map) {
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		Integer id = Integer.parseInt(map.get("id").toString());
		Integer productId = Integer.parseInt(map.get("productId").toString());
		String detail = (String)map.get("detail");
		Byte isDisplay = Byte.parseByte(map.get("isDisplay").toString());
		
		CfProductDetail productDetail = new CfProductDetail();
		productDetail.setId(id);;
		productDetail.setProductId(productId);
		productDetail.setDetail(detail);
		productDetail.setIsdisplay(isDisplay);
		productDetail.setUpdateTime(new Date());
		
		int i = detailService.updateProductDetail(productDetail);
		if (i > 0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("修改商品详情成功");
			cityFoodResult.setData(true);
		} else {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("修改商品详情失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
		
	// 根据商品详情信息删除商品详情,delete
	@RequestMapping(value = "/manage/product/detail", method = RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteProductDetail(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		CfProductDetail productDetail=new CfProductDetail();
		productDetail.setId(Integer.parseInt(id));
		int i = detailService.deleteProductDetail(productDetail);
		if (i > 0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("删除商品详情成功");
			cityFoodResult.setData(true);
		} else {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("删除商品详情失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	// 获取所有商品，入库管理用
	@RequestMapping(value = "/manage/allProducts", method = RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAllProducts(){
		CityFoodResult cityFoodResult = new CityFoodResult();
		List<CfProduct> list = productService.getAllProducts();
		List<AdminProductDisplay> apDisplayList = new ArrayList<AdminProductDisplay>();
		for (CfProduct product : list) {
				AdminProductDisplay apDisplay = new AdminProductDisplay();
				apDisplay.setProductId(product.getId());
				apDisplay.setName(product.getName());
				@SuppressWarnings("rawtypes")
				Map attributeList = null;
				if (product.getAttributeList()!=null) {
					attributeList = JsonUtils.jsonToMap(product.getAttributeList());
				}
				apDisplay.setAttribute(attributeList);
				apDisplayList.add(apDisplay);
		}
		if(list.size() > 0){
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("获取商品列表成功");
			cityFoodResult.setData(apDisplayList);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("获取商品列表为空");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	
	//前台查询---------------------------------------------------------------------------------
	
	//根据商品id获取商品评论
	@RequestMapping(value="/product/comment/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCommentByProductId(@PathVariable(value="id") Integer productId,
												@RequestParam(defaultValue="1,2,3,4,5") String commnetNum,
												@RequestParam(defaultValue="0") Integer isPic,
												@RequestParam(defaultValue="1") Integer currentPage){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		//商品评论展示包装类集合
		List<Map<String,Object>> ls = new ArrayList<>();

		

		List<Byte> numberStar = new ArrayList<>();
		String[] numberStr = commnetNum.split(",");
		for (String str : numberStr) {
			numberStar.add(Byte.parseByte(str));
		}
		PageInfo<CfComment> list = commentService.getCommentByProductIdAndPage(productId,numberStar,isPic, currentPage,10);
		if (list!=null) {
			for (CfComment comment : list.getList()) {
				if (comment.getIsdisplay()!=0) {
					//商品评论展示包装类
					Map<String,Object> ms = new LinkedHashMap<>();
					CfUser user = userService.getUserByUserId(comment.getUserId());
					ms.put("id", comment.getId());
					ms.put("userPic", user.getAvatarurl());
					ms.put("nickName", user.getNickname());
					ms.put("commentNum", comment.getCommentNumber());
					ms.put("content", comment.getContent());
					List<String> picList = new ArrayList<>();
					if (comment.getCommentImage()!=null) {
						String[] strs = comment.getCommentImage().split(",");
						for (String s : strs) {
							picList.add(s);
						}
					}
					ms.put("contentPic", picList);
					ms.put("commentDate", sd.format(comment.getCreateTime()));
					ls.add(ms);
				}
			}
			
			//星级统计数
			int oneStarNum = 0;
			int twoStarNum = 0;
			int threeStarNum = 0;
			int fourStarNum = 0;
			int fiveStarNum = 0;
			//带图片评论统计数
			int havePicNum = commentService.statCommentsHavePic(productId);
			
			List<CfComment> commentByProductId = commentService.getCommentByProductId(productId);
			for (CfComment comm : commentByProductId) {
				if (comm.getCommentNumber()==1) {
					//1星级加1
					oneStarNum=oneStarNum+1;
				}else if(comm.getCommentNumber()==2){
					//2星级加1
					twoStarNum=twoStarNum+1;
				}else if(comm.getCommentNumber()==3){
					threeStarNum=threeStarNum+1;
				}else if(comm.getCommentNumber()==4){
					fourStarNum=fourStarNum+1;
				}else if(comm.getCommentNumber()==5){
					fiveStarNum=fiveStarNum+1;
				}
				
			}
			
			List<CommentStat> tabs = new ArrayList<>();
			tabs.add(new CommentStat("一星",oneStarNum));
			tabs.add(new CommentStat("二星",twoStarNum));
			tabs.add(new CommentStat("三星",threeStarNum));
			tabs.add(new CommentStat("四星",fourStarNum));
			tabs.add(new CommentStat("五星",fiveStarNum));
			tabs.add(new CommentStat("有图",havePicNum));
			
			Map<String,Object> mp = new LinkedHashMap<>();
			//存入查询到的总数
			mp.put("total", list.getTotal());
            mp.put("tabs",tabs);
			//存入需要展示的数据
			mp.put("comments", ls);

			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询商品评论成功");
			cityFoodResult.setData(mp);
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setMsg("暂无商品评价哦~");
		cityFoodResult.setData("{}");
		return cityFoodResult;
	}
	
	//前台查询---------------------------------------------------------------------------------
	
		//根据商品id获取商品评论
		@RequestMapping(value="/product/more/{id}",method=RequestMethod.GET)
		@ResponseBody
		public CityFoodResult getMoreByProductId(@PathVariable(value="id") Integer productId,
												@RequestParam(defaultValue="1") Integer currentPage){
			
			CityFoodResult cityFoodResult = new CityFoodResult();
			//首页商品展示包装类
			List<SimpleProduct> Products = new ArrayList<SimpleProduct>();
			
			CfProduct byProductId = productService.getProductByProductId(productId);
			if (byProductId==null) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("暂无更多推荐");
				cityFoodResult.setData("{}");
				return cityFoodResult;
			}
			if (byProductId.getIsdisplay()==0) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("此商品已下架");
				cityFoodResult.setData("{}");
				return cityFoodResult;
			}
			Map<String,Object> pMap = new LinkedHashMap<>();
			pMap.put("categoryId", byProductId.getCategoryId());
			PageInfo<CfProduct> productList = productService.getProductByCategoryIdAndPage(pMap,currentPage,10);
			if (productList.getTotal()==0) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("暂无更多推荐");
				cityFoodResult.setData("{}");
				return cityFoodResult;
			}else{
				List<Map<String,Object>> ls = new ArrayList<>();
			for (CfProduct product : productList.getList()) {
				if (product.getIsdisplay()!=0&&product.getId()!=productId) {
					
					Map<String,Object> mp = new LinkedHashMap<>();
					mp.put("foodId", product.getId());
					mp.put("foodName", product.getName());
					mp.put("foodTitle", product.getSubTitle());
					mp.put("pic", product.getMainImage());
					List<CfProductSpecs> list = specsService.getProductSpecsByProductId(product.getId());
					if (list!=null) {
						mp.put("price", list.get(0).getPrice().doubleValue());
					}
					ls.add(mp);
					
				}
				
			}
			Map<String,Object> mp = new LinkedHashMap<>();
			//存入查询到的总数
			mp.put("total", productList.getTotal());
			//存入需要展示的数据
			mp.put("mores", ls);
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询更多商品成功");
			cityFoodResult.setData(mp);
			return cityFoodResult;
		
			}
		}
	
	
}
